package com.yicheng.statistics.service

import java.util.Date
import java.util.concurrent.CountDownLatch

import scala.concurrent.Future
import scala.util.Failure
import scala.util.Success

import com.yicheng.statistics.common.AlarmDB
import com.yicheng.statistics.common.HTADB
import com.yicheng.statistics.common.RTAModel.AnalyseGroup
import com.yicheng.statistics.common.RTAModel.VehicleTiredSetting
import com.yicheng.statistics.common.TimeUtils
import com.yicheng.statistics.repo.RTAModel.BaseAlarm
import com.yicheng.statistics.repo.cassandra.DataInfoDB
import com.yicheng.statistics.repo.model.AlarmUtils
import com.yicheng.statistics.repo.model.Data.DataInfo
import scala.util.control.Exception.Catch

/**
  * Created by yuer on 2016/12/2.
  */
object HTAService {

  def getFirstAlarmData(alarmType:Int,first:Date,end:Date):Future[Seq[BaseAlarm]] ={
      alarmType match {
        case AlarmUtils.battery_alarm =>
          AlarmDB.getFirstAlarmDataBySource
        case typ:Int if(1000 to 1999) contains typ =>
          AlarmDB.getFirstAlarmDataByType(typ,first,end)
        case typ:Int if(10000 to 29999) contains typ =>
          AlarmDB.getFirstAlarmDataByType(typ,first,end)
      }
  }

  def getLastAlarmData(device_type:Int,device_id:String,alarm_stop:Date,alarmType:Int):Future[BaseAlarm] ={
    alarmType match {
      case typ:Int if(1000 to 1999) contains typ =>
        AlarmDB.getLastAlarmDataByType(device_type:Int,device_id:String,alarm_stop,typ)
      case typ:Int if(10000 to 21999) contains typ =>
        AlarmDB.getLastAlarmDataByType(device_type:Int,device_id:String,alarm_stop,typ)
    }
  }

  /** 根据设备获得分析组集合
    *
    * @param deviceId 设备Id
    * @param deviceTypeId 设备类型Id
    * @return Seq[AnalyseGroup] 分析组集合
    */
  def getAnalyseGroupsByDevice(deviceId:String,deviceTypeId: Int):Future[Seq[AnalyseGroup]] = {
        HTADB.getAnalyseGroupsByDevice(deviceId,deviceTypeId)
  }
  
  /**
    * 获得疲劳驾驶分析设置集合
    *
    * @param analyseGroupsID 分析组Id
    * @return Seq[VehicleTiredSetting] 疲劳驾驶分析设置集合
    */
  def getVehicleTiredSettings(analyseGroupsID:Long): Future[Seq[VehicleTiredSetting]] = {
    HTADB.getVehicleTiredSettings(analyseGroupsID:Long)
  }
  
  
  import scala.concurrent.ExecutionContext.Implicits.global
  /**
   * 获得设备一整天的数据
   */
  def getOneDayDataInfo(deviceType:Int, deviceId:String ) : Future[List[DataInfo]] = {
    val dataInfos: List[DataInfo]  = List()
    
    try{
      
      val cdl = new CountDownLatch(24)
      (0 to 23).map(//0点开始到23点
        t => {
          val time = TimeUtils.getStartEndTimeByHour(t, 1)//取开始时间和结束时间(前一天)
          DataInfoDB.list(deviceType, deviceId, time._1.toDate(), time._2.toDate()) onComplete {
            case Success(result) =>
              result foreach { 
                ai => dataInfos :+ ai//追加数据
              }
              cdl.countDown()
            case Failure(error) =>
              println("查询数据失败"+error)
            
          }
        }
      )
      cdl.await()
      
    }catch{
      case ex:Exception => 
        println(ex.getMessage)
    }
    Future{dataInfos}
  }

}
